﻿myApp.controller("SaleLetController", function ($scope, $http, $modal, modalService, $routeParams, $location) {
    document.title = "Penang Realty BUY OR RENT WITH US 世界地產 SELL LET";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty BUY OR RENT WITH US 世界地產 SELL LET', 'description': 'Let us help you sell or rent your property FAST!.' });

    $scope.clearEntry = function () {
        $scope.property.submissionType = "Sale";
        $scope.property.address = "";
        $scope.property.name = "";
        $scope.property.price = "";
        $scope.property.email = "";
        $scope.property.remark = "";
        $scope.property.phone = "";
        $scope.property.company = "";
        $scope.data.images = null;
        angular.element("input[type='file']").val(null);
    }

    $scope.save = function () {
        $scope.clearEntry();

        if (!validation())
            return false;

        var formData = new FormData();
        //formData.append('image', image.file);
        angular.forEach($scope.data.images, function (image) {
            formData.append('image', image.resized.dataURL);
        })
       
        formData.append('property', angular.toJson($scope.property));
        $http.post('api/SaleLet/save', formData, {
            headers: { 'Content-Type': undefined },
            transformRequest: angular.identity
        }).success(function (result) {
            $scope.info = [];
            $scope.info ="Thanks for your submission. Your data is saved successfully!";
            $scope.clearEntry();
        }).error(function (result) {
            console.log("Error");
            console.log(result);
            $scope.error = "Your request cannot be submitted now. Please contact our office directly for us to serve you immediately.";
        });
    }

    function validateEmail(email) {
        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        return re.test(email);
    }

    function validation() {
        $scope.warning = [];
              
        if (!$scope.property.submissionType)
            $scope.warning.push({ msg: "Submission type is not selected!" });
        if (!$scope.property.price)
            $scope.warning.push({ msg: "Budget must not be empty!" });
        if (!$scope.property.name)
            $scope.warning.push({ msg: "Name must not be empty!" });
        if (!$scope.property.phone)
            $scope.warning.push({ msg: "Phone Number must not be empty!" });
        if (!$scope.property.address)
            $scope.warning.push({ msg: "address must not be empty!" });
        if ($scope.property.email) {
            if (!validateEmail($scope.property.email))
                $scope.warning.push({ msg: "Email format is not correct!" });
        }
        if ($scope.data.images) {
            if ($scope.data.images.length > 3)
                $scope.warning.push({ msg: "Maximum 3 images!" });
        }

        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }
})